package com.anran.netty.server.handler;

import com.anran.netty.message.GroupChatRequestMessage;
import com.anran.netty.message.GroupChatResponseMessage;
import com.anran.netty.server.session.GroupSession;
import com.anran.netty.server.session.GroupSessionFactory;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.List;

/**
 * 群聊发送处理器
 */
@ChannelHandler.Sharable
public class GroupChatRequestMessageHandler extends SimpleChannelInboundHandler<GroupChatRequestMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, GroupChatRequestMessage msg) throws Exception {
        GroupSession groupSession = GroupSessionFactory.getGroupSession();

        List<Channel> channels = groupSession.getMembersChannel(msg.getGroupName());

        channels.forEach(item->{
            item.writeAndFlush(new GroupChatResponseMessage(msg.getFrom(),msg.getContent()));
        });
    }
}
